package util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @classname:DateUtil
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/2 22:52
 * @version: 0.1
 * @since:1.8
 */
public class DateUtil {
    private DateUtil(){}
    //static修饰的是工具类
    public static int getDuration(String borrowTime,String returnTime, String pattern){
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            Date returnDateTime  = dateFormat.parse(returnTime);
            Date borrowDateTime  = dateFormat.parse(borrowTime);

            //小时转换成小时
            long duringDate = returnDateTime.getTime()-borrowDateTime.getTime();
            //除1000变成毫秒
            //除3600变成小时
            return (int)duringDate/3600/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
